<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";
import {decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      parent_id: {label:'父级菜单',name:'parent_id',type:'select_one',config:[],tips:'',value: 0},
      // name: {label:'菜单名称',name:'name',type:'text_one',tips:'只允许英文数字和下划线'},
      title: {label:'菜单标题',name:'title',type:'text_one',tips:'建议长度1-20'},
      head_img: {label:'头图',name:'head_img',type:'file_img',tips:''},
      path: {label:'路径',name:'path',type:'text_one',tips:''},
      unique: {label:'去重标识',name:'unique',type:'text_one',tips:'用于去重的标识，具有相同去重标识的多个菜单，只会展现第一个可用的菜单。解决用权限区分多个菜单时，超级管理员下同时显示多个菜单的问题'},
      // url: {label:'URL',name:'url',type:'text_one',tips:''},
      // param: {label:'参数',name:'param',type:'text_one',tips:''},
      right: {label:'权限需求',name:'right',type:'select_more',config:[],tips:''},
      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'排序，从大到小',value:50},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/menu/edit?id=`+this.id : '/admin/menu/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('menu/index', '$refs.list_url.list');
          iframe.hide('menu/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/menu/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/menu/list?where[parent_id][eq]=0',cache:true},(res)=>{
      let parent_id_config = decode_config(res.data.list, {content_key:'title', value_key:'id'});
      parent_id_config.unshift({content:'顶级菜单', value: '0'})
      this.config.parent_id.config = parent_id_config;
      return false
    })
    ajax({url:'/admin/admin_role/rights',cache:true},(res)=>{
      this.config.right.config=res.data;
      return false
    })
  }
}
</script>
